﻿using DayuCode.Designer.Common;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms.template
{
    public partial class FormPageLayout : UIForm
    {
        string terminalType;
        long? pageLayoutId;
        TemplatePageLayout templatePageLayout;
        public FormPageLayout(string terminalType, long? pageLayoutId)
        {
            InitializeComponent();
            cmbStatus.Items.Add(new CodeItem("1", "可用"));
            cmbStatus.Items.Add(new CodeItem("0", "不可用"));
            this.terminalType = terminalType;
            this.pageLayoutId = pageLayoutId;
            if(pageLayoutId != null)
            {
                templatePageLayout = TemplatePageLayoutHandler.getTemplatePageLayout((long)pageLayoutId);
            }
            else
            {
                templatePageLayout = new TemplatePageLayout();
                templatePageLayout.TerminalType = terminalType;
            }
            
            
            txtLayoutName.Text = templatePageLayout.LayoutName;
            if (templatePageLayout.Status == 1) cmbStatus.SelectedIndex = 1;
            else cmbStatus.SelectedIndex = 0;
            txtOrdnum.Text = "" + templatePageLayout.Ordnum;
            txtImgPath.Text = templatePageLayout.ImgPath;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string layoutName = txtLayoutName.Text.Trim();
            if (string.IsNullOrWhiteSpace(layoutName))
            {
                this.ShowErrorTip("布局名称不能为空");
                return;
            }
            int status = Cast.ToInt32((cmbStatus.SelectedItem as CodeItem).Code, 0);
            templatePageLayout.LayoutName = layoutName;
            templatePageLayout.Status = status;
            templatePageLayout.Ordnum = Cast.ToInt32(txtOrdnum.Text, 0);
            templatePageLayout.ImgPath = txtImgPath.Text.Trim();

            templatePageLayout = TemplatePageLayoutHandler.saveTemplatePageLayout(templatePageLayout);

            Close();
            DialogResult = DialogResult.OK;
        }

        public TemplatePageLayout TemplatePageLayout
        {
            get { return templatePageLayout; }
        }
    }
}
